﻿var bootList = [
    [bootDialogs, "Loading excuses..."],
    [bootPastLogin, "Conferring with mother..."],
    [bootNewLogin, "Asking for papers..."],
    [bootLoadSites, "Painting the sky..."],
    [bootDashboard, "Querying IQueryables..."],
    [bootFirstView, "Go!"]
];
var bootPos = 0;

function bootFirstView(callback) {
    view.show($(".content.sites"));
    if (callback != null) callback();
}

function bootDashboard(callback)
{
    doUpdate(callback);
}

function bootUI() {
    if (bootPos >= bootList.length) {
        updateBoot('100%', "Spreading the love...");
        hideDialog();
        return;
    }
    var perc = ((bootPos) * 100) / bootList.length;
    updateBoot(perc + '%', bootList[bootPos][1]);
    bootList[bootPos][0](function () { bootPos++; bootUI(); });
}

function bootPastLogin(callback) {
    if (readCookie('wamsUI.auth') != null) checkLogin(function (isLoggedIn) {
        if (!isLoggedIn) eraseCookie("wamsUI.auth");
        if (callback != null) callback();
    });
    else if (callback != null) callback();
}

function bootNewLogin(callback) {
    if (readCookie('wamsUI.auth') == null) requestLogin(callback);
    else if (callback != null) callback();
}

function bootLoadSites(callback) {
    $.ajax({
        type: 'GET',
        contentType: 'text/html',
        url: 'sites.html'
    })
    .done(function (data, status, xhr) {
        $("#mainview").append(data);
        callback();
    }).fail(function (xhr, status, error) {
        stopError("Error while trying to load sites html." + status);
    });

}

function bootDialogs(callback) {
    $("#dialogSpace").show();
    $.ajax({
        type: 'GET',
        contentType: 'text/html',
        url: 'dialogs.html'
    })
    .fail(function (xhr, status, error) {
        stopError("Error while trying to load dialogs. " + status);
    })
    .done(function (data, status, xhr) {
        $("#dialogSpace").append($(data));
        callback();
    });
    
}
function updateBoot(progress, title) {
    var msgDiv = $("#booterDialog .msg");
    msgDiv.html(title);
    $("#booterDialog .progress .bar").animate({ width : progress},200);
}
